//
//  ContentViewController.m
//  Eath
//
//  Created by yuhaya on 10/4/14.
//  Copyright (c) 2014 yuhaya. All rights reserved.
//

#import "ContentViewController.h"
#import "Tool.h"
#import "graph.h"
#import "picture.h"
#import "Database.h"


@interface ContentViewController ()

@end

@implementation ContentViewController

- (id)initWhithStu:(Student *)stu andTime:(NSUInteger)time
{
    self = [super init];  // Call a designated initializer here.
    if (self != nil) {
        self.stu = stu;
        self.time = time;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    NSDate * time = [NSDate dateWithTimeIntervalSince1970:self.time];
    [customLab setText:[Tool getDateStringWithDate:time DateFormat:@"yyyy-MM-dd"]];
    customLab.textAlignment = NSTextAlignmentCenter;
    customLab.font = [UIFont boldSystemFontOfSize:16];
    self.navigationItem.titleView = customLab;
    
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.frame = CGRectMake(0, 0, 120, 350);
    self.scrollView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
    
    //人物头像
    UIImage * img = [UIImage imageNamed:self.stu.photo];
    UIImageView * img_view = [[UIImageView alloc] initWithImage:img];
    img_view.frame = CGRectMake(50, 20, 50, 50);
    img_view.layer.cornerRadius = img_view.frame.size.width/2;
    img_view.layer.shouldRasterize = YES;
    img_view.layer.masksToBounds = YES;
    [img_view.layer setBorderWidth:3];
    [img_view.layer setBorderColor:[Tool colorWithHexString:@"#ffffff"].CGColor];
    img_view.layer.shadowColor = [Tool colorWithHexString:@"#cccccc"].CGColor;
    img_view.layer.shadowOpacity = 1.0;
    img_view.layer.shadowRadius = 5.0;
    img_view.layer.shadowOffset = CGSizeMake(0, 2);
    img_view.clipsToBounds = NO;
    [self.scrollView addSubview:img_view];
    
    //学生姓名
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(120, 15, 80, 30)];
    label.text = self.stu.name;
    UIFont * font = [UIFont systemFontOfSize:20.0];
    label.font = font;
    [self.scrollView addSubview:label];
    
    //学生学校年级信息
    UILabel * label_edu_msg = [[UILabel alloc] initWithFrame:CGRectMake(120, 50, 150, 20)];
    label_edu_msg.text = self.stu.grade;
    UIFont * font3 = [UIFont boldSystemFontOfSize:13.0];
    [label_edu_msg setTextColor:[UIColor grayColor]];
    label_edu_msg.font = font3;
    [self.scrollView addSubview:label_edu_msg];
    
    //显示时间信息
        
    UILabel * label_time = [[UILabel alloc] initWithFrame:CGRectMake(200, 22, 100, 20)];
    label_time.text = [Tool getDateStringWithDate:[NSDate dateWithTimeIntervalSince1970:self.time] DateFormat:@"yyyy-MM-dd"];
    UIFont * font4 = [UIFont italicSystemFontOfSize:12.0];
    label_time.textColor = [UIColor grayColor];
    label_time.font = font4;
    [self.scrollView addSubview:label_time];
    
    
    //画板
    self.pinter = [[graph alloc] initWithFrame:CGRectMake(0, 70, 320, 750) andStu:self.stu andTime:self.time];
    UISwipeGestureRecognizer * recognizer_right = [[UISwipeGestureRecognizer alloc]initWithTarget:self.tabs action:@selector(handleSwipeFrom:)];
    [recognizer_right setDirection:(UISwipeGestureRecognizerDirectionRight)];
    UISwipeGestureRecognizer * recognizer_left = [[UISwipeGestureRecognizer alloc]initWithTarget:self.tabs action:@selector(handleSwipeFrom:)];
    [recognizer_left setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [self.pinter addGestureRecognizer:recognizer_left];
    [self.pinter addGestureRecognizer:recognizer_right];
    
    self.stu.delegate = self;
    [self.stu all_mes:self.time];
    //接受数据跟新后刷新界面显示
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(flashPinter) name:SYNC_ALL_MESSAGE object:nil];
    

    [self.scrollView addSubview:self.pinter];
    self.scrollView.contentSize = CGSizeMake(320, self.pinter.frame.size.height + 50);
    self.view = self.scrollView;
}



/**
 *  刷新界面显示
 */
- (void)flashPinter
{
   [self.stu all_mes:self.time];
}

- (void)small_pic_click:(picture *)sender
{
    NSMutableArray * imgs = [[Database sharedDatabase] studentPointByDataId:sender.point_id];
    if(imgs == nil || [imgs count] == 0){
        [Tool showMessage:@"图片正在上传到云端，请稍后再试！"];
        return;
    }
//    DLog(@"%d",[self.pinter.img_point[sender.index] integerValue]);
//    DLog(@"%@",imgs);
    [self.detegate showImgWith:imgs andIndex:0];
}

-(void) stuPointOver:(NSMutableArray *)data
{
    if([data count] > 0){
    
        DLog(@"%@",data);
        DLog(@"a");
    }
    
    NSMutableArray * date_point = [NSMutableArray array];
    NSMutableArray * school_point = [NSMutableArray array];
    NSMutableArray * point_id = [NSMutableArray array];
    NSMutableArray * has_img = [NSMutableArray array];
    NSMutableArray * equipment_status = [NSMutableArray array];
    
    for (NSDictionary * tmp in data) {
        [date_point addObject:tmp[@"time"]];
        [school_point addObject:tmp[@"school"]];
        [point_id addObject:tmp[@"id"]];
        [has_img addObject:tmp[@"hasimg"]];
        [equipment_status addObject:tmp[@"status"]];
    }
    self.pinter.date_point = date_point;
    self.pinter.school_point = school_point;
    self.pinter.equipment_status = equipment_status;
    //设置所有的坐标
    [self.pinter set_time_point];
    self.pinter.frame = CGRectMake(0, 70, 320, [self.pinter.position_total count] * SPACE + 100);
    
    //重新渲染的时候删除所有的子view
    NSArray *views = [self.pinter subviews];
    
    for(picture* view in views)
    {
        [view removeFromSuperview];
    }
    [self.pinter setNeedsDisplay];
    //删除子view结束
    
    NSMutableArray * position = self.pinter.position_point;
    
    for (int a = 0; a < position.count; a++) {
        
        picture * p_img_view = [picture buttonWithType:UIButtonTypeCustom];
        [p_img_view setBackgroundImage:[UIImage imageNamed:@"default_img"] forState:UIControlStateNormal];
        
        NSNumber * tmp_position = position[a];
        NSInteger y = tmp_position.integerValue;
        p_img_view.frame = CGRectMake(210, y-10, 20, 20);
        
        p_img_view.clipsToBounds = NO;
        [p_img_view addTarget:self action:@selector(small_pic_click:) forControlEvents:UIControlEventTouchUpInside];
        [[NSNotificationCenter defaultCenter] addObserver:p_img_view selector:@selector(changeShow:) name:SYNC_POINT_IMGS object:nil];
        
        p_img_view.point_id = [point_id objectAtIndex:a];
        if([[has_img objectAtIndex:a] integerValue] == 0){
            p_img_view.hidden = YES;
        }
        [self.pinter addSubview:p_img_view];
    }
    
}

-(void) stuPointFail
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
